package com.bsit.obu.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.method.DigitsKeyListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


import com.bsit.obu.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PromptDialog extends Dialog {

	@BindView(R.id.win_prompt_title)
	protected TextView titleView;
	@BindView(R.id.win_prompt_content)
	protected EditText contentView;
	private String title;
	private String content;
	protected View view;
	protected onSubmitListener onSubmit;
	Context context;
	private boolean mOnlyInputNum;
	int dataLength = -1;
	public PromptDialog(Context context, String title, String content,
						onSubmitListener onSubmit,int i, boolean isNum) {
		super(context, R.style.dialog);
		dataLength = i;
		this.context = context;
		this.content = content;
		this.title = title;
		this.onSubmit = onSubmit;
		this.mOnlyInputNum = isNum;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.dialog_prompt, null);
		ButterKnife.bind(this, view);
		if (mOnlyInputNum){
			DigitsKeyListener numericOnlyListener = new DigitsKeyListener(false,true);
			contentView.setKeyListener(numericOnlyListener);
			if (dataLength != -1) {
				contentView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(dataLength)});
			}
//			contentView.setInputType(InputType.TYPE_CLASS_NUMBER);
		}
		if (title != null && !title.equals(""))
			titleView.setText(title);

		if (content != null && !content.equals(""))
			contentView.setText(content);
		this.setContentView(view);
	}

	@OnClick(R.id.win_prompt_submit)
	protected void submit(){
		onSubmit.onSubmit(contentView.getText().toString().trim());
	}

	@OnClick(R.id.win_prompt_cancle)
	protected void cancle(){
		onSubmit.onCancle();
	}

	public interface onSubmitListener{
		void onSubmit(String text);
		void onCancle();
	}
}
